package com.matchmaking.system.service.impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.matchmaking.system.config.MapConverter;
import com.matchmaking.system.domain.District;
import com.matchmaking.system.domain.SysDept;
import com.matchmaking.system.mapper.DistrictMapper;
import com.matchmaking.system.service.DistrictService;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class DistrictServiceImpl implements DistrictService {
    @Autowired
    DistrictMapper districtMapper;


    @Override
    public List<District> selectObjects(District district) {
        return districtMapper.selectObjects(district);
    }

    @Override
    public Integer updateObjects(District district) {
        return districtMapper.updateObjects(district);
    }


    @Override
    public Integer removeObjects(Integer id) {
        return districtMapper.removeObjects(id);
    }

    @Override
    public List<District> findexcharDistrict() {
        return districtMapper.findexcharDistrict();
    }

    @Override
    public List<District> findFirstLetter() {
        return districtMapper.findFirstLetter();
    }

    @Override
    public List<District> leveldists() {
        return districtMapper.leveldists();
    }

    @Override
    public void updateStatus(District district) {
        districtMapper.updateStatus(district);
    }

    @Override
    public List<District> DistrictExport(District district) {
        return districtMapper.DistrictExport(district);
    }

    @Override
    public void exprot(List<District> districts, HttpServletResponse response) throws IOException {

        try {

            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            //这里是URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("地区数据", "UTF-8");
            response.setHeader("Content-disposition","attachment;filename=" +  fileName + ".xlsx");

            //这里需要设置不关闭流
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            //设置背景颜色
            headWriteCellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
            //设置头字体
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short)13);
            headWriteFont.setBold(true);
            headWriteCellStyle.setWriteFont(headWriteFont);

            //设置头居中
            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            //内容策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            //设置水平居中
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            //内容自动换行
            contentWriteCellStyle.setWrapped(true);
            //设置文本收缩至合适
            contentWriteCellStyle.setShrinkToFit(true);

            //垂直居中
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置左边框
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            //设置右边框
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            //设置上边框
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            //设置下边框
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);



            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

            EasyExcel.write("D:/pic/DQ.xlsx", District.class)
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerConverter(new MapConverter())
                    .autoCloseStream(Boolean.TRUE).sheet("记录表")
                    .doWrite(districts);



        }catch (Exception e){

        }



}

    @Override
    public District selectCityNameLevel(District district) {
        return districtMapper.selectCityNameLevel(district);
    }

    @Override
    public Integer insertObject(District district) {
        return districtMapper.insertObject(district);
    }

//    @Override
//    public Integer insertObjects(District district) {
//        return districtMapper.insertObjects(district);
//    }


}
